package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.service.IReportService;
import com.yeshine.opendata.utils.CommonUtil;
import com.yeshine.opendata.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by taohj on 2014/10/8.
 */

@Controller
@RequestMapping("/admin/reports")
public class AdminReportsController extends AbstractAdminControllerBase {

    @Autowired
    private IReportService service;





    @RequestMapping("orgDatasetsV")
    public String orgDatasetsV() {

        return "admin/reports/orgDatasets";
    }

    @ResponseBody
    @RequestMapping("orgDatasets")
    public  String orgDatasets(){
        return  JsonUtil.toString(service.orgDatasetsReport());
    }



    @RequestMapping("appthemeAppsV")
    public String appthemeAppsV() {

        return "admin/reports/appthemeApps";
    }


    @ResponseBody
    @RequestMapping("appthemeApps")
    public  String appthemeApps(){
        return  JsonUtil.toString(service.appthemeAppsReport());
    }


    @RequestMapping("visitsV")
    public String visitsV() {

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("type",3);


        Calendar   cal_1= Calendar.getInstance();//获取当前日期

        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天



        map.put("starttime",CommonUtil.dateFormat(cal_1.getTime(),"yyyy-MM-dd"));
        map.put("endtime",CommonUtil.dateFormat(new Date(),"yyyy-MM-dd"));

        getRequest().setAttribute("param",map);

        return "admin/reports/visits";
    }


    @ResponseBody
    @RequestMapping("getVisitsStatistics")
    public String getVisitsStatistics(
            @RequestParam(value = "starttime",required = false) String starttime,
            @RequestParam(value = "endtime",required = false) String endtime,
            @RequestParam(value = "type",required = false) String type
    ){
        Map<String,Object> map=new HashMap<String, Object>();

        if(StringUtils.hasLength(type) && StringUtils.hasLength(starttime) && StringUtils.hasLength(endtime)){
            map.put("type",type);
            map.put("starttime",CommonUtil.dateParse(starttime,"yyyy-MM-dd"));
            map.put("endtime", CommonUtil.dateParse(endtime, "yyyy-MM-dd"));
        }else{
            map.put("type",3);
            Calendar   cal_1= Calendar.getInstance();//获取当前日期
            cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            map.put("starttime",cal_1.getTime());
            map.put("endtime", new Date());
        }

        return JsonUtil.toString(service.getVisitsStatistics(map));
    }






    @ResponseBody
    @RequestMapping("getDataDownloadStatistics")
    public String getDataDownloadStatistics(
            @RequestParam(value = "starttime",required = false) String starttime,
            @RequestParam(value = "endtime",required = false) String endtime,
            @RequestParam(value = "type",required = false) String type
    ){
        Map<String,Object> map=new HashMap<String, Object>();

        if(StringUtils.hasLength(type) && StringUtils.hasLength(starttime) && StringUtils.hasLength(endtime)){
            map.put("type",type);
            map.put("starttime",CommonUtil.dateParse(starttime,"yyyy-MM-dd"));
            map.put("endtime", CommonUtil.dateParse(endtime, "yyyy-MM-dd"));
        }else{
            map.put("type",3);
            Calendar   cal_1= Calendar.getInstance();//获取当前日期
            cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            map.put("starttime",cal_1.getTime());
            map.put("endtime", new Date());
        }

        return JsonUtil.toString(service.getDataDownloadStatistics(map));
    }


    @RequestMapping("getUserlastLoginStatisticsV")
    public String getUserlastLoginStatisticsV() {


        Map<String,Object> map=new HashMap<String, Object>();
        map.put("type",3);


        Calendar   cal_1= Calendar.getInstance();//获取当前日期

        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天



        map.put("starttime",CommonUtil.dateFormat(cal_1.getTime(),"yyyy-MM-dd"));
        map.put("endtime",CommonUtil.dateFormat(new Date(),"yyyy-MM-dd"));

        getRequest().setAttribute("param",map);


        return "admin/reports/dataVisits";
    }







    @ResponseBody
    @RequestMapping("getUserlastLoginStatistics")
    public String getUserlastLoginStatistics(
            @RequestParam(value = "starttime",required = false) String starttime,
            @RequestParam(value = "endtime",required = false) String endtime,
            @RequestParam(value = "type",required = false) String type
    ){
        Map<String,Object> map=new HashMap<String, Object>();

        if(StringUtils.hasLength(type) && StringUtils.hasLength(starttime) && StringUtils.hasLength(endtime)){
            map.put("type",type);
            map.put("starttime",CommonUtil.dateParse(starttime,"yyyy-MM-dd"));
            map.put("endtime", CommonUtil.dateParse(endtime, "yyyy-MM-dd"));
        }else{
            map.put("type",3);
            Calendar   cal_1= Calendar.getInstance();//获取当前日期
            cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            map.put("starttime",cal_1.getTime());
            map.put("endtime", new Date());
        }

        return JsonUtil.toString(service.getUserlastLoginStatistics(map));
    }







    @RequestMapping("datadownloadsV")
    public String datadownloadsV() {

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("type",3);


        Calendar   cal_1= Calendar.getInstance();//获取当前日期

        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天



        map.put("starttime",CommonUtil.dateFormat(cal_1.getTime(),"yyyy-MM-dd"));
        map.put("endtime",CommonUtil.dateFormat(new Date(),"yyyy-MM-dd"));

        getRequest().setAttribute("param",map);


        return "admin/reports/datadownloads";
    }


    public static void main(String[] args) {
        Calendar   cal_1= Calendar.getInstance();//获取当前日期

        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天

        System.out.println("-----1------firstDay:"+CommonUtil.dateFormat(cal_1.getTime(),"yyyy-MM-dd"));
    }

}
